<?php
namespace app\controller;

use app\BaseController;
use app\Request;
use think\facade\Cache;
use app\model\User;
use app\model\UserTest;
use think\Loader;

class Index extends BaseController
{
    public function hello(Request $request){
        return $request->param('uid');
    }

    public function inserExcel(){

         Loader::import('PHPExcel.Classes.PHPExcel');
         Loader::import('PHPExcel.Classes.PHPExcel.IOFactory.PHPExcel_IOFactory');
         Loader::import('PHPExcel.Classes.PHPExcel.Reader.Excel5');
                 //获取表单上传文件
         $file = request()->file('excel');
         $info = $file->validate(['ext' => 'xlsx'])->move(ROOT_PATH . 'public' . DS . 'uploads');
         if ($info) {
             //echo $info->getFilename();
             $exclePath = $info->getSaveName();  //获取文件名
             $file_name = ROOT_PATH . 'public' . DS . 'uploads' . DS . $exclePath;   //上传文件的地址
             $objReader =\PHPExcel_IOFactory::createReader('Excel2007');
             $obj_PHPExcel =$objReader->load($file_name, $encode = 'utf-8');  //加载文件内容,编码utf-8
             echo "<pre>";
             $excel_array=$obj_PHPExcel->getsheet(0)->toArray();   //转换为数组格式
             array_shift($excel_array);  //删除第一个数组(标题);
             $city = [];
             foreach($excel_array as $k=>$v) {
                                 $city[$k]['Id'] = $v[0];
                 $city[$k]['code'] = $v[1];
                 $city[$k]['path'] = $v[2];
                 $city[$k]['pcode'] = $v[3];
                 $city[$k]['name'] = $v[4];
             }
             //Db::name('city')->insertAll($city); //批量插入数据
         } else {
             echo $file->getError();
         }

    }
    public function test(){
        try {

        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }

    public function index()
    {

        $usertest = new UserTest();
        $user = new User();
        $list = $usertest->select()->toArray();

        foreach ($list as $v){
            $data = [];
            $data['deleted'] = 1;
            if($v['deleted'] == '1'){
                //$res = $user->where('id','=',$v['id'])->save($data);
            }
        }

        //return date('Y-m-d',1596211200);
        //phpinfo();
        //Cache::store('redis')->set('name','value116',3600);
        // 获取存储的数据并输出
        //echo "Stored string in redis:: " . Cache::store('redis')->get("name");
        //return '<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Title</title></head><body><form action="intoexcel" enctype="multipart/form-data" method="post"><input type="file" name="excel" /><input type="submit" value="导入"></form></body></html>';
    }



}
